import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";
import { Base } from "@/entities.base/base.entity";
import { Order } from "../order.entity";
import { CustomerPoints } from "../customer-points.entity";

@Entity({ comment: '订单积分使用记录表', name: 'basis_order_points_usage' })
export class OrderPointsUsage extends Base {

    /** 使用积分金额 */
    @Column({
        comment: '使用积分金额',
        type: 'decimal',
        precision: 10,
        scale: 2,
    })
    usedPointsAmount: number;

    /** 抵扣金额 */
    @Column({
        comment: '抵扣金额',
        type: 'decimal',
        precision: 10,
        scale: 2,
    })
    deductionAmount: number;

    /** 积分兑换比例 */
    @Column({
        comment: '积分兑换比例', // 例如：100积分=1元，则比例为0.01
        type: 'decimal',
        precision: 10,
        scale: 4,
        default: 0.01,
    })
    exchangeRate: number;

    /** 备注 */
    @Column({
        type: 'text',
        comment: '备注',
        nullable: true,
    })
    remark?: string;

    /** 关联的订单ID */
    @Column({
        comment: '订单ID',
        nullable: false,
    })
    orderId: string;

    /** 关联的客户积分批次ID */
    @Column({
        comment: '客户积分批次ID',
        nullable: false,
    })
    customerPointsId: string;

    /** 关联订单 - 多对一关系 */
    @ManyToOne(() => Order, order => order.pointsUsages, { onDelete: 'CASCADE' })
    @JoinColumn({ name: 'orderId' })
    order: Order;

    /** 关联客户积分批次 - 多对一关系 */
    @ManyToOne(() => CustomerPoints, { onDelete: 'CASCADE' })
    @JoinColumn({ name: 'customerPointsId' })
    customerPoints: CustomerPoints;
} 